"use strict";
const { app, BrowserWindow, screen, ipcMain, shell } = require("electron");
const path = require("path");
const fs = require("fs");
const os = require("os");
const multiparty = require("multiparty");
const createWindow = () => {
  ipcMain.on("open_youdao", (event, arg) => {
    if (arg.word) CreatYoudaoWindow(arg.word);
  });
  ipcMain.on("open_renjiao", (event, arg) => {
    let renjiao = new BrowserWindow({
      width: 1024,
      height: 768,
      title: "有道翻译",
      webPreferences: {
        nodeIntegration: true,
        contextIsolation: false,
        preload: path.join(__dirname, "tools/renjiao.js")
      }
    });
    renjiao.setMenu(null);
    renjiao.loadURL(arg);
  });
  ipcMain.on("on_link", (event, arg) => {
    shell.openExternal(arg);
  });
  const win = new BrowserWindow({
    width: 1125,
    height: 680,
    //transparent: true,
    //frame: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      webviewTag: true
    }
  });
  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
  } else {
    win.loadFile("dist/index.html");
  }
  win.on("close", () => {
    app.quit();
  });
  win.webContents.openDevTools();
  win.setMenu(null);
  setTimeout(() => {
  }, 5e3);
  let on_msg = new BrowserWindow({
    width: 1e3,
    height: 1e3,
    transparent: true,
    frame: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  });
  let { width, height } = screen.getPrimaryDisplay().workAreaSize;
  on_msg.setPosition(width / 2 - 500, 0);
  on_msg.setIgnoreMouseEvents(true);
  on_msg.setAlwaysOnTop(true);
  on_msg.setSkipTaskbar(true);
  on_msg.setMenu(null);
  on_msg.webContents.openDevTools();
  if (process.env.VITE_DEV_SERVER_URL) {
    on_msg.loadURL(process.env.VITE_DEV_SERVER_URL + "#/on-msg");
  } else {
    on_msg.loadFile("dist/index.html", { hash: "/on-msg" });
  }
  const ctg = new BrowserWindow({
    width: 300,
    height: 1e3,
    transparent: true,
    frame: false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  });
  if (process.env.VITE_DEV_SERVER_URL) {
    ctg.loadURL(process.env.VITE_DEV_SERVER_URL + "#/class-table-gui");
  } else {
    ctg.loadFile("dist/index.html", { hash: "/class-table-gui" });
  }
  ctg.setMenu(null);
  ctg.setAlwaysOnTop(true);
  ctg.setSkipTaskbar(true);
  let { width: w, height: h } = screen.getPrimaryDisplay().workAreaSize;
  ctg.setPosition(w - 290, h / 2 - 500);
  ctg.setIgnoreMouseEvents(true);
  setInterval(() => {
    let is_top = on_msg.isAlwaysOnTop() && ctg.isAlwaysOnTop();
    if (!is_top) {
      on_msg.setAlwaysOnTop(true);
      ctg.setAlwaysOnTop(true);
    }
  }, 1e3);
};
const CreatYoudaoWindow = (word) => {
  let youdao = new BrowserWindow({
    width: 1024,
    height: 768,
    title: "有道翻译",
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      preload: path.join(__dirname, "tools/youdao.js")
    }
  });
  youdao.setMenu(null);
  youdao.loadURL("https://www.youdao.com/result?word=" + word + "&lang=en");
};
app.whenReady().then(() => {
  const http = require("http");
  const express = require("express");
  const cors = require("cors");
  let app2 = express();
  app2.use(cors());
  let server = http.createServer(app2);
  let io = require("socket.io")(server);
  app2.get("/api/tools/youdao", function(req, res) {
    console.log(req.query);
    let word = req.query["word"];
    fetch("https://dict.youdao.com/suggest?num=5&ver=3.0&doctype=json&cache=false&le=en&q=" + word).then((res2) => res2.json()).then((data) => {
      res.json(data);
    }).catch((err) => {
      res.json({
        code: 500,
        msg: "fail",
        data: {}
      });
    });
  });
  app2.post("/api/file_upload", function(req, res) {
    let form = new multiparty.Form();
    form.uploadDir = path.join(os.homedir(), "jiao-gong-file");
    form.parse(req, function(err, fields, file) {
      console.log(file);
      res.send("数据已接收");
    });
  });
  io.on("connection", function(socket) {
    console.log("有新客户端连接");
    socket.on("disconnect", function() {
      console.log("客户端断开");
    });
  });
  try {
    server.listen(11451, function() {
      console.log("server is running at port 11451");
      let homePath = os.homedir();
      fs.mkdirSync(path.join(homePath, "jiao-gong-file"), { recursive: true });
      createWindow();
    });
  } catch (e) {
    dialog.showErrorBox("错误", "启动失败，请检查端口是否被占用，请重启电脑后重试");
    app2.quit();
  }
});
